package saucaca.fragment;

import java.util.ArrayList;

import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;

import org.json.JSONException;

import saucaca.CustomView.CircularImageView;
import saucaca.CustomView.FilteredArrayAdapter;
import saucaca.Database.DataBase;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.AlbumObject;
import saucaca.Object.SearchObject;
import saucaca.Object.SingerObject;
import saucaca.Object.SongObject;
import saucaca.Util.PreferenceUtils;
import saucaca.Util.TagName;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class LocalAllMusic extends Fragment {
	private static final String TAG = LocalAllMusic.class.getSimpleName();
	public static final String SONGS_MUSIC = "allmusic";
	public static final String ARTIST_MUSIC = "singermusic";
	public static final String ALBUM_MUSIC = "albummusic";
	public static final String FILE_MUSIC = "filemusic";
	public static final String PLAYLIST_MUSIC = "playlistmusic";
	public static final String VIDEO_MUSIC = "videomusic";

	CircularImageView acountImage;
	TextView txtAllSong, txtBaihat, txtNghesy, txtAlbum, txtPlaylist, txtFolders, txtVideo;
	LinearLayout layoutBaihat, layoutNghesy, layoutAlbum, layoutPlaylist, layoutFolders, layoutVideo, layoutOnlineYeuthich, layoutOnlinePlaylist;
	public static DataBase db;
	MainActivity mActivity;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	@SuppressLint("NewApi")
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// return super.onCreateView(inflater, container, savedInstanceState);
//		getActivity().getActionBar().hide();
		View view = inflater.inflate(R.layout.song_local, container, false);
		findViewById(view);
		
		String strAvatar=PreferenceUtils.getString(mActivity, TagName.KeyPreference_avatar);
		if(strAvatar!=null){
			Bitmap bitmap=BaseEngine.BitmapFromBase64(strAvatar);
			acountImage.setImageBitmap(bitmap);
		}
		
		db = new DataBase(mActivity);
		txtAllSong.setText(Integer.toString(mActivity.mMusicList.size()) + " " + mActivity.getResources().getString(R.string.number_song));
		txtBaihat.setText(Integer.toString(mActivity.allMusicMap.size()));
		txtNghesy.setText(Integer.toString(mActivity.singerMusicMap.size()));
		txtAlbum.setText(Integer.toString(mActivity.albumMusicMap.size()));
		txtFolders.setText(Integer.toString(mActivity.fileMusicMap.size()));
		txtPlaylist.setText(Integer.toString(mActivity.playListMusicMap.size()));
		txtVideo.setText(Integer.toString(mActivity.allVideoMap.size()));

		layoutBaihat.setOnClickListener(clickListener);
		layoutAlbum.setOnClickListener(clickListener);
		layoutFolders.setOnClickListener(clickListener);
		layoutNghesy.setOnClickListener(clickListener);
		layoutPlaylist.setOnClickListener(clickListener);
		layoutVideo.setOnClickListener(clickListener);

		layoutOnlineYeuthich.setOnClickListener(clickListener);
		layoutOnlinePlaylist.setOnClickListener(clickListener);
		mActivity.myActionBar.setMenu(R.string.menu_nhaccuatoi, false, true);
		return view;
	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if(hidden){
			String strAvatar=PreferenceUtils.getString(mActivity, TagName.KeyPreference_avatar);
			if(strAvatar!=null){
				Bitmap bitmap=BaseEngine.BitmapFromBase64(strAvatar);
				acountImage.setImageBitmap(bitmap);
			}
		}
		mActivity.myActionBar.setMenu(R.string.menu_nhaccuatoi, false, true);
	}

	OnClickListener clickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String contentFragment = LocalListFragment.class.getName();
			switch (v.getId()) {
			case R.id.offline_layout_baihat:
				switchFragment(contentFragment, SONGS_MUSIC);
				break;
			case R.id.offline_layout_album:
				switchFragment(contentFragment, ALBUM_MUSIC);
				break;
			case R.id.offline_layout_folders:
				switchFragment(contentFragment, FILE_MUSIC);
				break;
			case R.id.offline_layout_nghesy:
				switchFragment(contentFragment, ARTIST_MUSIC);
				break;
			case R.id.offline_layout_playlist:
				switchFragment(contentFragment, PLAYLIST_MUSIC);
				break;
			case R.id.offline_layout_video:
				switchFragment(contentFragment, VIDEO_MUSIC);
				break;
			case R.id.online_layout_playlist:
				String user = PreferenceUtils.getString(getActivity(), TagName.KeyPreference_user);
				if (user != null)
					mActivity.switchContent(OnlinePlayListFragment.class.getName());
				else {
					new AlertDialog.Builder(mActivity).setTitle("Bạn chưa đăng nhập!").setMessage("Hãy đăng nhập để add playlist!")
							.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									mActivity.switchContent(AccountLoginFragment.class.getName());
								}
							}).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									// do nothing
								}
							}).setIcon(android.R.drawable.ic_dialog_alert).show();
				}
				break;
			case R.id.online_layout_baihatyeuthich:
				// switchFragment(contentFragment, VIDEO_MUSIC);
				break;
			default:
				break;
			}

		}
	};
	private void switchFragment(String fragment, String data) {
		if (mActivity == null) {
			return;
		} else if (mActivity instanceof MainActivity) {
			MainActivity a = (MainActivity) mActivity;
			a.switchContent(fragment, data);
		}
	}

	private void findViewById(View view) {
		txtAllSong = (TextView) view.findViewById(R.id.offline_mymusic_nubmer);
		txtBaihat = (TextView) view.findViewById(R.id.offline_text_baihat);
		txtAlbum = (TextView) view.findViewById(R.id.offline_text_album);
		txtNghesy = (TextView) view.findViewById(R.id.offline_text_nghesy);
		txtVideo = (TextView) view.findViewById(R.id.offline_text_video);
		txtPlaylist = (TextView) view.findViewById(R.id.offline_text_playlist);
		txtFolders = (TextView) view.findViewById(R.id.offline_text_folders);
		acountImage=(CircularImageView) view.findViewById(R.id.account_avatar);
		
		layoutBaihat = (LinearLayout) view.findViewById(R.id.offline_layout_baihat);
		layoutAlbum = (LinearLayout) view.findViewById(R.id.offline_layout_album);
		layoutFolders = (LinearLayout) view.findViewById(R.id.offline_layout_folders);
		layoutNghesy = (LinearLayout) view.findViewById(R.id.offline_layout_nghesy);
		layoutPlaylist = (LinearLayout) view.findViewById(R.id.offline_layout_playlist);
		layoutVideo = (LinearLayout) view.findViewById(R.id.offline_layout_video);

		layoutOnlinePlaylist = (LinearLayout) view.findViewById(R.id.online_layout_playlist);
		layoutOnlineYeuthich = (LinearLayout) view.findViewById(R.id.online_layout_baihatyeuthich);
		
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
	}


	@Override
	public void onStart() {
		super.onStart();
	}
}
